package com.xin.controller;

import com.xin.constant.MessageConstant;
import com.xin.entity.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.plugin.liveconnect.SecurityContextHelper;

/**
 * @Author 夜天
 * @Date 2021/12/21 17:46
 * @Version 1.0.0
 * @description： 用户相关
 */
@RestController
@RequestMapping("/user")
public class UserController {
    // 获取当前登录用户的用户名
    @RequestMapping("/getUserName")
    public Result getUserName(){
        try{
            org.springframework.security.core.userdetails.User user =
                    (org.springframework.security.core.userdetails.User)
                            SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true,MessageConstant.GET_USERNAME_SUCCESS,user.getUsername());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }
}
